package com.suredy.app.porject.ctrl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.suredy.app.porject.constants.ContractStatus;
import com.suredy.app.porject.constants.ContractType;
import com.suredy.app.porject.constants.FlowStatus;
import com.suredy.app.porject.ctrl.view.ViewRoleCtrl;
import com.suredy.app.porject.model.Bid;
import com.suredy.app.porject.model.Contract;
import com.suredy.app.porject.service.BidSrv;
import com.suredy.app.porject.service.ContractSrv;
import com.suredy.app.porject.service.FlowOfContract2UserSrv;
import com.suredy.core.ctrl.BaseCtrl;

/**
 * 合同列表
 * 
 * @author VIVID.G
 * @since 2016-1-21
 * @version v0.1
 */
@Controller
@RequestMapping("/contract-list")
public class ContractListCtrl extends BaseCtrl {

	@Autowired
	private BidSrv bidSrv;

	@Autowired
	private ContractSrv contractSrv;

	@Autowired
	private FlowOfContract2UserSrv contract2UserSrv;

	@RequestMapping(value = "/sign")
	public String signContract(Contract search, String projectNo, String qyf, String wcqk, Integer page, Integer pageSize, Model model) {
		String view = "app/contract/contract-sign-list";

		if (page == null || page < 1)
			page = 1;

		if (pageSize == null || pageSize < 1)
			pageSize = 30;

		DetachedCriteria dc = this.contractSrv.getDc(search);

		dc.add(Restrictions.in("type", ContractType.MAIN_CONTRACT_TYPES));
		dc.addOrder(Order.asc("flowStatus"));
		dc.addOrder(Order.asc("bjTime"));

		// 项目编号
		if (!StringUtils.isBlank(projectNo)) {
			Bid b = new Bid();
			b.setProjectNo(projectNo);
			DetachedCriteria child = this.bidSrv.getDc(b);
			child.setProjection((Property.forName("id")));

			dc.add(Property.forName("bid.id").in(child));

			model.addAttribute("projectNo", projectNo);
		}

		// 签约方
		if (!StringUtils.isBlank(qyf)) {
			Criterion in = Restrictions.and(Restrictions.in("type", ContractType.IN_MONEY_CONTRACT_TYPE), Restrictions.like("companyA", qyf, MatchMode.ANYWHERE));
			Criterion out = Restrictions.and(Restrictions.in("type", ContractType.OUT_MONEY_CONTRACT_TYPE), Restrictions.like("companyB", qyf, MatchMode.ANYWHERE));

			dc.add(Restrictions.or(in, out));

			model.addAttribute("qyf", qyf);
		}

		// 完成情况
		if (!StringUtils.isBlank(wcqk)) {
			if ("true".equals(wcqk)) {
				dc.add(Restrictions.ne("flowStatus", FlowStatus.FS2000_CONTRACT_TO_CREATE));
			} else {
				dc.add(Restrictions.eq("flowStatus", FlowStatus.FS2000_CONTRACT_TO_CREATE));
			}

			model.addAttribute("wcqk", wcqk);
		}

		String[] userIds = new String[] {this.getUser().getId()};

		List<Contract> data = this.contract2UserSrv.getContractOf(dc, userIds, true, page, pageSize, FlowStatus.FS2000_CONTRACT_TO_CREATE);

		model.addAttribute("data", data);
		model.addAttribute("search", search);
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.contract2UserSrv.getContractCountOf(dc, userIds, true, FlowStatus.FS2000_CONTRACT_TO_CREATE));

		return view;
	}

	@RequestMapping(value = "/view")
	public String doContractForShichang(Contract search, String projectNo, String qyf, String ssdw, Integer lxzt, Integer page, Integer pageSize, Model model) {
		String view = "app/contract/contract-view-list";

		if (page == null || page < 1)
			page = 1;

		if (pageSize == null || pageSize < 1)
			pageSize = 30;

		DetachedCriteria dc = this.contractSrv.getDc(search);

		dc.add(Restrictions.in("type", ContractType.MAIN_CONTRACT_TYPES));
		dc.addOrder(Order.asc("flowStatus"));
		dc.addOrder(Order.asc("bjTime"));

		// 项目编号
		if (!StringUtils.isBlank(projectNo)) {
			Bid b = new Bid();
			b.setProjectNo(projectNo);
			DetachedCriteria child = this.bidSrv.getDc(b);
			child.setProjection((Property.forName("id")));

			dc.add(Property.forName("bid.id").in(child));

			model.addAttribute("projectNo", projectNo);
		}

		// 签约方
		if (!StringUtils.isBlank(qyf)) {
			Criterion in = Restrictions.and(Restrictions.in("type", ContractType.IN_MONEY_CONTRACT_TYPE), Restrictions.like("companyA", qyf, MatchMode.ANYWHERE));
			Criterion out = Restrictions.and(Restrictions.in("type", ContractType.OUT_MONEY_CONTRACT_TYPE), Restrictions.like("companyB", qyf, MatchMode.ANYWHERE));

			dc.add(Restrictions.or(in, out));

			model.addAttribute("qyf", qyf);
		}

		// 实施单位
		if (!StringUtils.isBlank(ssdw)) {
			Criterion in = Restrictions.and(Restrictions.in("type", ContractType.IN_MONEY_CONTRACT_TYPE), Restrictions.like("companyB", ssdw, MatchMode.ANYWHERE));
			Criterion out = Restrictions.and(Restrictions.in("type", ContractType.OUT_MONEY_CONTRACT_TYPE), Restrictions.like("companyA", ssdw, MatchMode.ANYWHERE));

			dc.add(Restrictions.or(in, out));

			model.addAttribute("ssdw", qyf);
		}

		if (lxzt != null) {
			switch (lxzt) {
				case 0:
					dc.add(Restrictions.eq("status", ContractStatus.START));
					break;
				case 1:
					dc.add(Restrictions.in("status", new ContractStatus[] {ContractStatus.WAIT, ContractStatus.SUSPEND, ContractStatus.SHUTDOWN}));
					break;
				case 2:
					dc.add(Restrictions.in("status", new ContractStatus[] {ContractStatus.FINISH, ContractStatus.COMPLETED}));
					break;
			}

			model.addAttribute("lxzt", lxzt);
		}

		List<Contract> data = this.contract2UserSrv.getContractOf(dc, null, true, page, pageSize, FlowStatus.FS2001_CONTRACT_CREATED, FlowStatus.FS2002_CONTRACT_STARTED, FlowStatus.FS2003_CONTRACT_COMPLETED);

		model.addAttribute("data", data);
		model.addAttribute("search", search);
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.contract2UserSrv.getContractCountOf(dc, null, true, FlowStatus.FS2001_CONTRACT_CREATED, FlowStatus.FS2002_CONTRACT_STARTED, FlowStatus.FS2003_CONTRACT_COMPLETED));

		return view;
	}

	@RequestMapping(value = "/all")
	public String sjbAllContract(Contract search, Integer page, Integer pageSize, Model model) {
		String view = "app/contract/contract-all-list";

		if (page == null || page < 1)
			page = 1;

		if (pageSize == null || pageSize < 1)
			pageSize = 30;

		DetachedCriteria dc = this.contractSrv.getDc(search);

		dc.add(Restrictions.in("type", ContractType.MAIN_CONTRACT_TYPES));

		List<Contract> data = this.contract2UserSrv.getContractOf(dc, null, true, page, pageSize, FlowStatus.FS2001_CONTRACT_CREATED, FlowStatus.FS2002_CONTRACT_STARTED, FlowStatus.FS2003_CONTRACT_COMPLETED);

		model.addAttribute("data", data);
		model.addAttribute("search", search);
		model.addAttribute("roles", new ViewRoleCtrl(this.getRoles(), this.getUser()));
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.contract2UserSrv.getContractCountOf(dc, null, true, FlowStatus.FS2001_CONTRACT_CREATED, FlowStatus.FS2002_CONTRACT_STARTED, FlowStatus.FS2003_CONTRACT_COMPLETED));

		return view;
	}

	@RequestMapping(value = "/c-and-m")
	public String contractAndMoneyList(Contract search, Integer page, Integer pageSize, Model model) {
		String view = "app/contract/contract-c-and-m-list";

		if (page == null || page < 1)
			page = 1;

		if (pageSize == null || pageSize < 1)
			pageSize = 30;

		DetachedCriteria dc = this.contractSrv.getDc(search);

		dc.add(Restrictions.in("type", ContractType.MAIN_CONTRACT_TYPES));

		List<Contract> data = this.contract2UserSrv.getContractOf(dc, null, true, page, pageSize, FlowStatus.FS2001_CONTRACT_CREATED, FlowStatus.FS2002_CONTRACT_STARTED, FlowStatus.FS2003_CONTRACT_COMPLETED);

		model.addAttribute("data", data);
		model.addAttribute("search", search);
		model.addAttribute("roles", new ViewRoleCtrl(this.getRoles(), this.getUser()));
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.contract2UserSrv.getContractCountOf(dc, null, true, FlowStatus.FS2001_CONTRACT_CREATED, FlowStatus.FS2002_CONTRACT_STARTED, FlowStatus.FS2003_CONTRACT_COMPLETED));

		return view;
	}

}
